package com.foreknow.service.impl;

import com.foreknow.bean.Guestbook;
import com.foreknow.bean.User;
import com.foreknow.dao.GuestbookDao;
import com.foreknow.dao.impl.GuestbookDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.service.GuestbookService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GuestbookServiceImpl implements GuestbookService {

    @Override
    public boolean saveguestbook(Guestbook guestbook) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestdao = new GuestbookDaoImpl();
        guestdao.setConnection(conn);
        boolean isRigth = false;
        try {
            isRigth = guestdao.save(guestbook);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return isRigth;
    }

    @Override
    public Guestbook sid(int id) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestdao = new GuestbookDaoImpl();
        guestdao.setConnection(conn);
        Guestbook guestbook = new Guestbook();
        try {
            guestbook = guestdao.selectid(id);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return guestbook;
    }

    @Override
    public List<Object> sname(String name) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestdao = new GuestbookDaoImpl();
        guestdao.setConnection(conn);
        List<Object> list = null;
        try {
            list = guestdao.selectname(name);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return list;
    }

    @Override
    public boolean deleteid(int id) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestdao = new GuestbookDaoImpl();
        guestdao.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = guestdao.delete(id);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return isRight;
    }

    @Override
    public boolean updateid(String name, String phone, String email, String title, String content, String createdtime, int id) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestdao = new GuestbookDaoImpl();
        guestdao.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = guestdao.update(name,phone,email,title,content,createdtime,id);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return isRight;
    }

    public static void main(String[] args) {
        GuestbookServiceImpl guestbookService = new GuestbookServiceImpl();
        Guestbook guestbook = new Guestbook();
        guestbook = guestbookService.sid(12);
        System.out.println(guestbook.getName());
    }
}
